Repository navigation
fix(security): rate limit dan log IP pengunjung asli di balik Cloudflare - #7
Merged
Merged
Conversation
Production traffic is visitor -> Cloudflare -> Coolify proxy -> app, and with trust proxy = 1 req.ip is the Cloudflare edge address. Edges rotate per request and are shared by unrelated visitors, so every IP-keyed limiter (login, register, email, payment, referral) was effectively off: 12 demo logins in a row from one machine all passed on the live API. Add lib/clientIp.ts: use CF-Connecting-IP only when the sending hop is in Cloudflare's published ranges, otherwise the hop address. Both getRateLimitIp copies use it, and the referral, affiliate and payment routes stop trusting the first X-Forwarded-For entry, which the visitor controls. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Masalah
Lalu lintas production berjalan pengunjung → Cloudflare → proxy Coolify → app. Dengan
trust proxy = 1,req.ipadalah alamat edge Cloudflare, bukan alamat pengunjung. Edge Cloudflare berganti-ganti per request dan dipakai bersama banyak pengunjung, sehingga semua rate limit berbasis IP (login, register, email, pembayaran, referral) praktis tidak berfungsi. Di API live, 12 demo-login berturut-turut dari satu mesin semuanya lolos; log server mencatat IP seperti162.158.189.100(milik Cloudflare).Perbaikan
backend/src/lib/clientIp.ts:getClientIp(req)memakai headerCF-Connecting-IPhanya jika hop pengirimnya ada di rentang IP resmi Cloudflare (dari cloudflare.com/ips-v4 dan ips-v6, dicek dengannet.BlockList). Selain itu dipakai alamat hop, sehingga orang yang memanggil server origin langsung tidak bisa memalsukan IP.getRateLimitIp(core/middleware.tsdanlib/rateLimiters.ts) memakai helper ini.X-Forwarded-For. Entri itu dikendalikan pengunjung, jadi IP yang tercatat untuk klik referral, pendaftaran affiliate, dan pembayaran bisa dipalsukan.Catatan untuk reviewer
clientIp.ts).Tes
backend/src/lib/clientIp.test.ts(12 tes): header Cloudflare dipakai bila hop Cloudflare; diabaikan bila hop bukan Cloudflare;X-Forwarded-Fordiabaikan; header tidak valid; IPv4-mapped IPv6; IPv6 Cloudflare.npm run check: lint, typecheck, 466 tes (212 frontend + 254 backend), build: lolos.🤖 Generated with Claude Code